﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace System.Web.Mvc.Html
{
    public static class AccordionExtension
    {        
        public static string Accordion(this HtmlHelper htmlHelper, string objId, AccordionItem[] controllers)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("$(function(){");
            sb.Append("$('#" + objId + "').accordion({ header: 'h3' ,  autoHeight: false});");

            sb.Append("$.ajaxSetup ({ cache: false });");

            foreach (var ctrl in controllers)
            {
                sb.Append("$('#" + ctrl.HtmlObjId + "').load(\"" + ctrl.actionController + "\");");
            }

            sb.Append("});");

            sb.Append("</script>");

            sb.Append("<div id=\"" + objId + "\">");

            foreach (var ctrl in controllers)
            {
                if (ctrl.IsInRole)
                {
                    sb.Append("<div>");
                    sb.Append("<h3><a href=\"#\">" + ctrl.HtmlObjTitle + "</a></h3>");
                    sb.Append("<div id=\"" + ctrl.HtmlObjId + "\">");
                    sb.Append("</div>");
                    sb.Append("</div>");                    
                }
            }

            sb.Append("</div>");

            return sb.ToString();
        }

        public class AccordionItem
        {
            private string controller;

            private string htmlObjId;

            private string htmlObjTitle;

            private bool _IsInRole;

            public bool IsInRole
            {
                get { return this._IsInRole; }
                set { this._IsInRole = value; }
            }

            public string actionController
            {
                get { return this.controller; }
                set { this.controller = value; }
            }

            public string HtmlObjId
            {
                get { return this.htmlObjId; }
                set { this.htmlObjId = value; }
            }

            public string HtmlObjTitle
            {
                get { return this.htmlObjTitle; }
                set { this.htmlObjTitle = value; }
            }
        }
    }
}
